package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.dongdongshop.bo.AlipayQueryBo;
import com.dongdongshop.bo.AlipayTradeQueryBo;
import com.dongdongshop.config.AlipayConfig;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.OrderItemService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AlipayController {

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @DubboReference
    private OrderItemService orderItemService;



    /**
     *
     * @param WIDout_trade_no  商户订单号
     * @param WIDtotal_amount 付款金额
     * @param WIDsubject 订单名称
     * @param WIDbody 付款描述
     * @return
     */
    @RequestMapping("alipayTradePagePay")
    @ResponseBody
    public String alipayTradePagePay(String WIDout_trade_no, String WIDtotal_amount,
                                     String WIDsubject, String WIDbody){


        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDout_trade_no +"\","
                + "\"total_amount\":\""+ WIDtotal_amount +"\","
                + "\"subject\":\""+ WIDsubject +"\","
                + "\"body\":\""+ WIDbody +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        //输出
        return result;
    }

    /**
     *
     * @param WIDTQout_trade_no 商户订单号
     * @param WIDTQtrade_no 交易流水号
     * @return
     */
    @RequestMapping("alipayTradeQuery")
    @ResponseBody
    public AjaxResult alipayTradeQuery(String WIDTQout_trade_no,String WIDTQtrade_no) {
        //获得初始化的AlipayClient

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTQout_trade_no +"\","+"\"trade_no\":\""+ WIDTQtrade_no +"\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
            System.out.println(result);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        AlipayQueryBo alipayQueryBo = JSONUtil.toBean(result, AlipayQueryBo.class);
        AlipayTradeQueryBo response = alipayQueryBo.getAlipay_trade_query_response();
        //输出
        return AjaxResult.OK().setData(response);
    }

    /**
     *description 退款
     * @param WIDTRout_trade_no 商户订单号
     * @param WIDTRtrade_no 支付宝交易号
     * @param WIDTRrefund_amount 退款的金额
     * @param WIDTRrefund_reason 退款的原因
     * @param WIDTRout_request_no 标识一次退款请求
     * @return
     */
    @RequestMapping("alipayTradeRefund")
    @ResponseBody
    public String alipayTradeRefund(String WIDTRout_trade_no,String WIDTRtrade_no,String WIDTRrefund_amount,
                                    String WIDTRrefund_reason,String WIDTRout_request_no) {

        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTRout_trade_no +"\","
                + "\"trade_no\":\""+ WIDTRtrade_no +"\","
                + "\"refund_amount\":\""+ WIDTRrefund_amount +"\","
                + "\"refund_reason\":\""+ WIDTRrefund_reason +"\","
                + "\"out_request_no\":\""+ WIDTRout_request_no +"\"}");
        //将退款请求号存到redis中
        redisTemplate.boundValueOps("refund-"+WIDTRout_trade_no).set(WIDTRout_request_no);
        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        List<TbOrderItem> order = orderItemService.findOrderByOrderNoAndTradeNoAndStatus(WIDTRout_trade_no, WIDTRtrade_no);
        if (order.size() == 1) {

        }
        //输出
        return  result;
    }

    /**
     *description 退款查询
     * @param WIDRQout_trade_no 商户订单号
     * @param WIDRQtrade_no 支付宝交易号
     * @param WIDRQtrade_no 退款请求号
     * @return
     */
    @RequestMapping("alipayTradeFastpayRefundQuery")
    @ResponseBody
    public String alipayTradeFastpayRefundQuery (String WIDRQout_trade_no,String WIDRQtrade_no,String WIDRQout_request_no) {

        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDRQout_trade_no +"\","
                +"\"trade_no\":\""+ WIDRQtrade_no +"\","
                +"\"out_request_no\":\""+ WIDRQtrade_no +"\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        return result;
    }
}
